package moe.bogos.douro.api.common.capabilities;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;

public class CapMapProvider implements ICapabilityProvider, INBTSerializable
{
    @CapabilityInject(ICapMap.class)
    public static Capability<ICapMap> MAP_CAP = null;
    //private ICapMap mapCapability;
    private LazyOptional<ICapMap> optional;
    
    public CapMapProvider()
    {
        optional = LazyOptional.of(CapMap::new);
    }
    
    public static ICapMap get(PlayerEntity player)
    {
        return player.getCapability(MAP_CAP, null).orElse(null);
    }
    
    public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction facing)
    {
        return MAP_CAP.orEmpty(capability, optional);
    }
    
    public INBT serializeNBT()
    {
        return optional.orElse(null).saveNBTData();
    }
    
    public void deserializeNBT(INBT nbt)
    {
        optional.orElse(null).loadNBTData((CompoundNBT) nbt);
    }
}
